import logging

from config import BASE_HOST


class RegLoginApi:

    def __init__(self, session):
        self.session = session

    # 获取图片验证码接口
    def get_img_verify_code(self, r):
        url = BASE_HOST + '/common/public/verifycode1/{}'.format(r)
        return self.session.get(url=url)

    # 获取短信验证码接口
    def get_phone_verify_code(self, phone_v, imgVerifyCode_v, type_v='reg'):
        url = BASE_HOST + '/member/public/sendSms'
        form_dict = {
            "phone": phone_v,
            "imgVerifyCode": imgVerifyCode_v,
            "type": type_v
        }
        response = self.session.post(url=url, data=form_dict)
        logging.info(f"获取短信验证码结果为：{response.json()}")
        return response

    # 注册接口
    def user_register(self, phone_v, password_v, verifycode_v, phone_code_v, dy_server=None):
        url = BASE_HOST + '/member/public/reg'
        form_dict = {
            "phone": phone_v,
            "password": password_v,
            "verifycode": verifycode_v,
            "phone_code": phone_code_v,
            "dy_server": dy_server,
        }
        response = self.session.post(url=url, data=form_dict)
        logging.info(f"注册响应：{response.json()}")
        return response

    # 登录接口
    def user_login(self, keywords_v, password_v):
        url = BASE_HOST + '/member/public/login'
        form_dict = {
            "keywords": keywords_v,
            "password": password_v
        }
        response = self.session.post(url=url, data=form_dict)
        logging.info(f"登录响应：{response.json()}")
        return response
